package com.qinggeng.project.tool.file.controller;

import com.qinggeng.common.utils.poi.ExcelUtil;
import com.qinggeng.framework.aspectj.lang.annotation.Log;
import com.qinggeng.framework.aspectj.lang.enums.BusinessType;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.framework.web.page.TableDataInfo;
import com.qinggeng.project.tool.file.domain.SysFile;
import com.qinggeng.project.tool.file.service.ISysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件存储Controller
 *
 * @author qinggeng
 * @date 2020-02-11
 */
@RestController
@RequestMapping("${toolPath}/file")
public class SysFileController extends BaseController
{
    @Autowired
    private ISysFileService sysFileService;

    /**
     * 查询文件存储列表
     */
    @PreAuthorize("@ss.hasPermi('tool:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysFile sysFile)
    {
        startPage();
        List<SysFile> list = sysFileService.selectSysFileList(sysFile);
        return getDataTable(list);
    }

    /**
     * 导出文件存储列表
     */
    @PreAuthorize("@ss.hasPermi('tool:file:export')")
    @Log(title = "文件存储", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysFile sysFile)
    {
        List<SysFile> list = sysFileService.selectSysFileList(sysFile);
        ExcelUtil<SysFile> util = new ExcelUtil<SysFile>(SysFile.class);
        return util.exportExcel(list, "file");
    }


    /**
     * 删除文件存储
     */
//    @PreAuthorize("@ss.hasPermi('tool:file:remove')")
    @Log(title = "删除文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds)
    {
        sysFileService.deleteSysFileByIds(fileIds);
        return AjaxResult.success();
    }

    @Log(title = "根据文件路径删除文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/delFile")
    public AjaxResult delFile(String path)
    {
        sysFileService.deleteSysFileByPath(path);
        return AjaxResult.success();
    }


//    @PreAuthorize("@ss.hasPermi('tool:file:edit')")
    @Log(title = "上传文件", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam MultipartFile file){
        return AjaxResult.success(sysFileService.upload(file,true));
    }


    @PreAuthorize("@ss.hasPermi('tool:file:edit')")
    @Log(title = "上传文件", businessType = BusinessType.UPDATE)
    @PostMapping("/synchronize")
    public AjaxResult synchronize(){
        sysFileService.synchronize();
        return AjaxResult.success();
    }
}
